package top.zhan;

import top.zhan.adaptor.cls.Adaptor;
import top.zhan.adaptor.cls.TargetModule;
import top.zhan.bridge.ApplePhone;
import top.zhan.bridge.BlueColor;
import top.zhan.bridge.HWPhone;
import top.zhan.bridge.YellowColor;
import top.zhan.builder.Computer;
import top.zhan.builder.ComputerDirector;
import top.zhan.builder.HignComputerBuilder;
import top.zhan.builder.LowComputerBuilder;
import top.zhan.chain.*;
import top.zhan.command.*;
import top.zhan.composite.File;
import top.zhan.composite.Folder;
import top.zhan.composite.Root;
import top.zhan.decorator.BigBike;
import top.zhan.decorator.Bike;
import top.zhan.decorator.RSCBikeDecorator;
import top.zhan.decorator.TrumpetBikeDecorator;
import top.zhan.facade.IMessageManager;
import top.zhan.facade.MessageFacade;
import top.zhan.factory.AliPay;
import top.zhan.factory.Pay;
import top.zhan.factory.WechatPay;
import top.zhan.factory.abs.FactoryProducer;
import top.zhan.factory.abs.OrderFactory;
import top.zhan.factory.method.AliPayFactory;
import top.zhan.factory.method.PayFactory;
import top.zhan.factory.method.WeChatFactory;
import top.zhan.factory.simple.SimplePayFactory;
import top.zhan.flyweight.CloudWebSite;
import top.zhan.flyweight.Company;
import top.zhan.flyweight.WebSiteFactory;
import top.zhan.iterator.ICollection;
import top.zhan.iterator.Iterator;
import top.zhan.iterator.MyCollection;
import top.zhan.memento.RoleOriginator;
import top.zhan.memento.RoleStateCaretaker;
import top.zhan.observe.AnnaConcreteObserver;
import top.zhan.observe.BossConcreteSubject;
import top.zhan.observe.LWConcreteObserver;
import top.zhan.observe.Observer;
import top.zhan.prototype.Person;
import top.zhan.proxy.DigitalSell;
import top.zhan.proxy.DigitalSellProxy;
import top.zhan.proxy.DigitalSellReal;
import top.zhan.state.NewOrderState;
import top.zhan.state.OrderContext;
import top.zhan.state.PayOrderState;
import top.zhan.state.SendOrderState;
import top.zhan.strategy.*;
import top.zhan.template.AbstractProjectManager;
import top.zhan.template.PayServiceProjectManager;
import top.zhan.template.UserServiceProjectManager;

/**
 * @author shadow
 */
public class Main {

    public static void main(String[] args) {
        // 单例模式测试
//        SingletonLazy.getInstance().process();
//        SingletonHungry.getInstance().process();

        //工厂模式测试
        //简单工厂模式
//        Pay pay1 = SimplePayFactory.createPay("WECHAT PAY");
//        Pay pay2 = SimplePayFactory.createPay("Ali PAY");
//        pay1.unifiedOrder();
//        pay2.unifiedOrder();
        //工厂方法模式
//        PayFactory aliPayFactory = new AliPayFactory();
//        Pay aliPay = aliPayFactory.getPay();
//        aliPay.unifiedOrder();
//        PayFactory wechatPayFactory = new WeChatFactory();
//        Pay wechatPay = wechatPayFactory.getPay();
//        wechatPay.unifiedOrder();
        //抽象工厂模式
//        OrderFactory wechatOrderFactory = FactoryProducer.getFactory("WECHAT");
//        wechatOrderFactory.createPay().unifiedOrder();
//        wechatOrderFactory.createRefund().refund();
//        OrderFactory aliOrderFactory = FactoryProducer.getFactory("ALI");
//        aliOrderFactory.createPay().unifiedOrder();
//        aliOrderFactory.createRefund().refund();

        //建造者模式
//        ComputerDirector computerDirector = new ComputerDirector();
//        Computer lowComputer = computerDirector.create(new LowComputerBuilder());
//        Computer hignComputer = computerDirector.create(new HignComputerBuilder());
//        System.out.println(lowComputer.toString());
//        System.out.println(hignComputer.toString());

        //适配器模式
        //类的适配
//        TargetModule targetModule = new Adaptor();
//        targetModule.methodA();
//        targetModule.methodB();
//        targetModule.methodC();

        // 桥接模式
//        HWPhone hwPhone = new HWPhone(new BlueColor());
//        hwPhone.run();
//        ApplePhone applePhone = new ApplePhone(new YellowColor());
//        applePhone.run();

        // 组合模式
        //创建根文件夹
//        Root root1 = new Folder("C://");
//        //建立子文件夹
//        Root desktop = new Folder("桌面");
//        Root myComputer = new Folder("我的电脑");
//        Root javaFile = new Folder("HelloWorld.java");
//        //建立文件夹关系
//        root1.addFile(desktop);
//        root1.addFile(myComputer);
//        //建立文件关系
//        myComputer.addFile(javaFile);
//        //从0级开始展示，每下一级，多2条横线
//        root1.display(0);

        // 装饰器模式
//        Bike bike = new BigBike();
//        //搭配个RSC防爆胎
//        bike = new RSCBikeDecorator(bike);
//        //搭配了一个喇叭
//        bike = new TrumpetBikeDecorator(bike);
//        bike = new TrumpetBikeDecorator(bike);
//        System.out.println(bike.getDescription() + "，价格：" + bike.getPrice());

        // 代理模式
//        //真实对象的行为
//        DigitalSell realObj = new DigitalSellReal();
//        realObj.sell();
//        // 代理对象的行为
//        DigitalSell proxy = new DigitalSellProxy();
//        proxy.sell();

        // 外观模式
//        IMessageManager iMessageManager = new MessageFacade();
//        iMessageManager.pushMessage();

        //享元模式
//        WebSiteFactory factory = new WebSiteFactory();
//        CloudWebSite companySite1 = factory.getWebSiteByKey("企业官网");
//        companySite1.run(new Company("小滴课堂"));
//        CloudWebSite companySite2 = factory.getWebSiteByKey("企业官网");
//        companySite2.run(new Company("徐尧课堂"));
//        CloudWebSite companySite3 = factory.getWebSiteByKey("信息流");
//        companySite3.run(new Company("字节跳动"));
//        System.out.println("网站分类总数：" + factory.getWebSiteCategorySize());

        //策略模式
//        ProductOrder productOrder = new ProductOrder(800, 1,33);
//        PromotionContext promotionContext;
//        //不同策略算出不同的活动价格
//        // 没有活动
//        promotionContext = new PromotionContext(new NormalActivity());
//        double finalPrice = promotionContext.executeStrategy(productOrder);
//        System.out.println("NormalActivity = " + finalPrice);
//        // 折扣活动
//        promotionContext = new PromotionContext(new DiscountActivity(0.8));
//        finalPrice = promotionContext.executeStrategy(productOrder);
//        System.out.println("DiscountActivity = " + finalPrice);
//        // 优惠券折扣
//        promotionContext = new PromotionContext(new VoucherActivity(100));
//        finalPrice = promotionContext.executeStrategy(productOrder);
//        System.out.println("DiscountActivity = " + finalPrice);

        // 模板设计模式
//        AbstractProjectManager projectManager;
//        projectManager = new PayServiceProjectManager();
//        projectManager.processProject();
//        System.out.println("+=======================+");
//        projectManager = new UserServiceProjectManager();
//        projectManager.processProject();

        // 观察者模式
//        //创建一个主题：老板
//        BossConcreteSubject bossConcreteSubject = new BossConcreteSubject();
//        //创建观察者，就是摸鱼的同事
//        Observer lwObserver = new LWConcreteObserver();
//        Observer annaObserver = new AnnaConcreteObserver();
//        //建立对应的关系，老板这个主题被同时进行观察
//        bossConcreteSubject.addObserve(lwObserver);
//        bossConcreteSubject.addObserve(annaObserver);
//        //主题开始活动，里面会通知观察者（相当于发布消息）
//        bossConcreteSubject.doSomething();

        // 责任链模式
//        RiskControlManager firstControlManager = new FirstRiskControl("初级风控");
//        RiskControlManager SecondControlManager = new SecondRiskControl("中级风控");
//        RiskControlManager thirdControlManager = new ThirdRiskControl("高级风控");
//        //形成调用链路
//        firstControlManager.setSuperior(SecondControlManager);
//        SecondControlManager.setSuperior(thirdControlManager);
//        //调用
//        Request request1 = new Request();
//        request1.setRequestType(RequestType.TRANSFER.name());
//        request1.setMoney(100000);
//        firstControlManager.handlerRequest(request1);

        // 命令模式
//        //创建接受者，空调就是接受者
//        ConditionReceiver receiver = new ConditionReceiver();
//        //创建命令对象，设置命令的接受者
//        Command onCommand = new OnCommand(receiver);
//        Command offCommand = new OffCommand(receiver);
//        Command coolCommand = new CoolCommand(receiver);
//        Command warmCommand = new WarmCommand(receiver);
//        //创建请求者，把命令对象设置进去，app就是请求发起者
//        AppInvoke appInvoke = new AppInvoke();
//        appInvoke.setOnCommand(onCommand);
//        appInvoke.setOffCommand(offCommand);
//        appInvoke.setCoolCommand(coolCommand);
//        appInvoke.setWarmCommand(warmCommand);
//        //使用
//        appInvoke.on();
//        System.out.println("+---------+");
//        appInvoke.cool();
//        System.out.println("+---------+");
//        appInvoke.warm();
//        System.out.println("+---------+");
//        appInvoke.off();

        // 迭代器模式
//        ICollection collection = new MyCollection();
//        collection.add("小滴课堂老王");
//        collection.add("小滴课堂Anna小姐姐");
//        collection.add("小滴课堂二当家小D");
//        collection.add("小滴课堂刘一手");
//        collection.add("小滴课堂老帆");
//        Iterator iterator = collection.iterator();
//        while (iterator.hasNext()){
//            Object obj1 = iterator.next();
//            System.out.println(obj1);
//        }

        // 备忘录模式
//        RoleOriginator role = new RoleOriginator();
//        role.display();
//        role.fight();
//        role.display();
//        System.out.println("保存上面的快照");
//        RoleStateCaretaker caretaker = new RoleStateCaretaker();
//        caretaker.setRoleStateMemento(role.roleSave());
//        role.fight();
//        role.fight();
//        role.fight();
//        role.fight();
//        role.fight();
//        role.display();
//        System.out.println("准备恢复快照");
//        role.recoveryState(caretaker.getRoleStateMemento());
//        role.display();

        // 状态模式
        OrderContext orderContext = new OrderContext();
        orderContext.setState(new NewOrderState());
        orderContext.setState(new PayOrderState());
        orderContext.setState(new SendOrderState());
    }
}

